﻿
using AutoMapper;
using D.Traceability.Domain;
using D.Traceability.ErrorCode;
using D.Traceability.Infrastructure.Infrastructure;
using D.Traceability.Write.API.APPlicant.Command;
using D.Traceability.Write.API.APPlicant.Command.TestCommand;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace D.Traceability.Write.API.APPlicant.CommandHandler.TestCommandHandler
{
    /// <summary>
    ///  删除质检报道图片
    /// </summary>
    public class DeleteTestCommandHandler : IRequestHandler<DeleteTestCommand, APIResult<string>>
    {
        private readonly IBaseRepository<SU_PictureModel> baseRepository;
        private readonly IMapper mapper;

        public DeleteTestCommandHandler(IBaseRepository<SU_PictureModel> baseRepository, IMapper mapper)
        {
            this.baseRepository = baseRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 删除质检报道图片
        /// </summary>
        public async Task<APIResult<string>> Handle(DeleteTestCommand request, CancellationToken cancellationToken)
        {
            APIResult<string> result = new APIResult<string>();
            try
            {
                //查询到本条图片
                var list = await baseRepository.GetAll().Result.FirstOrDefaultAsync(x => x.TraceaBatch == request.TraceaBatch && x.PTyper == request.PTyper && x.Url == request.Url);
                list.TraceaIsDelete = true;
                int res = await baseRepository.Update(list);
                if (res > 0)
                {
                    result.Code = APIEnum.删除成功;
                    result.Msg = APIEnum.删除成功.ToString();
                    result.data = res.ToString();
                }
                else
                {
                    result.Code = APIEnum.删除失败;
                    result.Msg = APIEnum.删除失败.ToString();
                    result.data = res.ToString();
                }
                return await Task.FromResult(result);

            }
            catch (Exception)
            {
                result.Code = APIEnum.删除失败;
                result.Msg = APIEnum.删除失败.ToString();
                return await Task.FromResult(result);
            }
        }
    }
}
